package com.ruoyi.wgjlcData.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.pad.ChartLineDTO;
import com.ruoyi.common.core.pad.LinePoint;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.gpsPoint.GpsPoint;
import com.ruoyi.wgjlcData.domain.plat.DataWgjlcZxtgbj;
import com.ruoyi.wgjlcData.domain.vo.TestZxtgbj;
import com.ruoyi.wgjlcData.mapper.DataWgjlcZxtgbjMapper;
import com.ruoyi.wgjlcData.service.IDataWgjlcZxtgbjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;


/**
 * 无轨胶轮车最小通过半径（激光\GPS）测试项Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-27
 */
@Service
public class DataWgjlcZxtgbjServiceImpl implements IDataWgjlcZxtgbjService
{
    @Autowired
    private DataWgjlcZxtgbjMapper dataWgjlcZxtgbjMapper;

    /**
     * 查询无轨胶轮车最小通过半径（激光\GPS）测试项
     * 
     * @param zxtgbjId 无轨胶轮车最小通过半径（激光\GPS）测试项主键
     * @return 无轨胶轮车最小通过半径（激光\GPS）测试项
     */
    @Override
    public DataWgjlcZxtgbj selectDataWgjlcZxtgbjByZxtgbjId(Long zxtgbjId)
    {
        return dataWgjlcZxtgbjMapper.selectDataWgjlcZxtgbjByZxtgbjId(zxtgbjId);
    }

    /**
     * 查询无轨胶轮车最小通过半径（激光\GPS）测试项列表
     * 
     * @param dataWgjlcZxtgbj 无轨胶轮车最小通过半径（激光\GPS）测试项
     * @return 无轨胶轮车最小通过半径（激光\GPS）测试项
     */
    @Override
    public List<DataWgjlcZxtgbj> selectDataWgjlcZxtgbjList(DataWgjlcZxtgbj dataWgjlcZxtgbj)
    {
        return dataWgjlcZxtgbjMapper.selectDataWgjlcZxtgbjList(dataWgjlcZxtgbj);
    }

    /**
     * 新增无轨胶轮车最小通过半径（激光\GPS）测试项
     * 
     * @param dataWgjlcZxtgbj 无轨胶轮车最小通过半径（激光\GPS）测试项
     * @return 结果
     */
    @Override
    public int insertDataWgjlcZxtgbj(DataWgjlcZxtgbj dataWgjlcZxtgbj)
    {
        dataWgjlcZxtgbj.setCreateTime(DateUtils.getNowDate());
        return dataWgjlcZxtgbjMapper.insertDataWgjlcZxtgbj(dataWgjlcZxtgbj);
    }

    /**
     * 修改无轨胶轮车最小通过半径（激光\GPS）测试项
     * 
     * @param dataWgjlcZxtgbj 无轨胶轮车最小通过半径（激光\GPS）测试项
     * @return 结果
     */
    @Override
    public int updateDataWgjlcZxtgbj(DataWgjlcZxtgbj dataWgjlcZxtgbj)
    {
        dataWgjlcZxtgbj.setUpdateTime(DateUtils.getNowDate());
        //list转Json字符串
        if(!dataWgjlcZxtgbj.getTestZxtgbjList().isEmpty()) {
            dataWgjlcZxtgbj.setDataList(JSON.toJSONString(dataWgjlcZxtgbj.getTestZxtgbjList()));
        }
        return dataWgjlcZxtgbjMapper.updateDataWgjlcZxtgbj(dataWgjlcZxtgbj);
    }

    /**
     * 查询无轨胶轮车-最小通过半径（激光\GPS）测试项
     *
     * @param dataTaskId
     * @return 无轨胶轮车-最小通过半径（激光\GPS）测试项
     */
    @Async("threadPoolKy")
    @Override
    public Future<List<DataWgjlcZxtgbj>> selectDataWgjlcZxtgbjByDataTaskId(Long dataTaskId) {
        List<DataWgjlcZxtgbj> dataWgjlcZxtgbjEntities = dataWgjlcZxtgbjMapper.selectDataWgjlcZxtgbjByDataTaskId(dataTaskId);
        if (dataWgjlcZxtgbjEntities.size() != 0) {
            dataWgjlcZxtgbjEntities.forEach(item -> {
                if (item.getDataList() != null && item.getDataList().length() != 0) {
                    item.setTestZxtgbjList(JSON.parseArray(item.getDataList(), TestZxtgbj.class));
                    item.setListSize(item.getTestZxtgbjList().size());
                    if(!item.getTestZxtgbjList().isEmpty()) {
                        GpsPoint gpsPoint = new GpsPoint();
                        item.getTestZxtgbjList().forEach(s-> {
                            ChartLineDTO chartLineDto = new ChartLineDTO();
                            if(!s.getJwdList().isEmpty()) {
                                List<LinePoint> zbPoint = gpsPoint.addPointF(s.getJwdList());
                                //最大半径
                                double maxRadius = gpsPoint.getMaxRadius(s.getJwdList());
                                s.setZbList(zbPoint);
                                List<Float> xVelList = new ArrayList<>(); List<Float> yVelList = new ArrayList<>();
                                for (LinePoint linePoint : s.getZbList()) {
                                    xVelList.add(linePoint.getX());
                                    yVelList.add(linePoint.getY());
                                }
                                // 曲线数值点集合
                                List<LinePoint> linePointList = new ArrayList<>();
                                // 最大值
                                LinePoint maxYLinePoint = new LinePoint();
                                // 实例化数据点集合，得到Y值最大、最小的数据点
                                for (int i = 0; i < zbPoint.size(); i++) {
                                    float thisY = yVelList.get(i);
                                    float thisX = xVelList.get(i);
                                    LinePoint thisP = new LinePoint(thisX, thisY);
                                    linePointList.add(thisP);
                                }
                                // 最大值
                                maxYLinePoint.setX((float)maxRadius);
                                maxYLinePoint.setY(0F);
                                chartLineDto.setMaxYLinePoint(maxYLinePoint);
                                chartLineDto.setLinePointList(linePointList);
                                s.setJwdChartLineDTO(chartLineDto);
                            }
                        });
                    }
                }
            });
        }
        return new AsyncResult<>(dataWgjlcZxtgbjEntities);
    }

    /**
     * 修改状态为2
     *
     * @param dataTaskId
     */
    @Override
    public int updateDataWgjlcZxtgbjByDataTaskId(Long dataTaskId) {
        return dataWgjlcZxtgbjMapper.updateDataWgjlcZxtgbjByDataTaskId(dataTaskId);
    }

    /**
     * 批量新增无轨胶轮车-最小通过半径（激光\GPS）测试项
     *
     * @param dataWgjlcZxtgbj 无轨胶轮车-最小通过半径（激光\GPS）测试项
     * @return 结果
     */
    @Override
    public int batchDataWgjlcZxtgbj(List<DataWgjlcZxtgbj> dataWgjlcZxtgbj) {
        return dataWgjlcZxtgbjMapper.batchDataWgjlcZxtgbj(dataWgjlcZxtgbj);
    }

    /**
     * 批量删除无轨胶轮车最小通过半径（激光\GPS）测试项
     * 
     * @param zxtgbjIds 需要删除的无轨胶轮车最小通过半径（激光\GPS）测试项主键
     * @return 结果
     */
    @Override
    public int deleteDataWgjlcZxtgbjByZxtgbjIds(Long[] zxtgbjIds)
    {
        return dataWgjlcZxtgbjMapper.deleteDataWgjlcZxtgbjByZxtgbjIds(zxtgbjIds);
    }

    /**
     * 删除无轨胶轮车最小通过半径（激光\GPS）测试项信息
     * 
     * @param zxtgbjId 无轨胶轮车最小通过半径（激光\GPS）测试项主键
     * @return 结果
     */
    @Override
    public int deleteDataWgjlcZxtgbjByZxtgbjId(Long zxtgbjId)
    {
        return dataWgjlcZxtgbjMapper.deleteDataWgjlcZxtgbjByZxtgbjId(zxtgbjId);
    }
}
